const express = require('express');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const userServer = require('../services/userService');

var router = express.Router();


router.use('/loginPage.html',async function (req,res,next) {
    var user = req.cookies.user;
    if(user== {}||user==undefined){
        console.log('cookies is null');
        next();
    }
    else if(user.role=='admin'){
        console.log('role admin isn\'t auto login');
        next();
    }else {
        //自动登录
        var data = await userServer.loginCookie(user);
        console.log("role user auto login!");
        if(data.success){
            req.session.user = data.user;
            //这个过程是客户端发出的信息
            res.redirect('/views/pages/user1.html');
        }else {
            next();
        }
    }
});

//对pages里的东西进行控制
router.use('/pages',function (req,res,next) {
    var user = req.session.user;
    console.log('/pages');
    if(user==[]||user=={}||user==undefined||user==''){
        res.redirect('/views/loginPage.html');
    }else {
        next();
    }
});

router.use('/admin',function (req,res,next) {
    var user = req.session.user;
    console.log('/admin');
    if(user==[]||user=={}||user==undefined||user==''){
        res.redirect('/views/loginPage.html');
    }else if(user.role=='user'){
        res.redirect('/views/loginPage.html');
    }else {
        next();
    }
});

//在views路由上做登录使用控制
//要进入pages下面的页面必须是已登录状态，否则全部进入/views/loginPage.html
// router.use('/pages',(req,res,next)=>{
//     var user = req.session['user'];
//     if(user==null||user=={}||user==undefined||user==''){
//         res.redirect('/views/loginPage.html')
//     }else {
//         next();
//     }
// });

// router.use('/',function (req,res,next) {
//     console.log('view /');
//     next();
// })

exports.router = router;